package org.fujene.sandbox;

import java.io.IOException;

import org.msgpack.MessagePackObject;
import org.msgpack.object.RawType;
import org.msgpack.rpc.Client;
import org.msgpack.rpc.Future;
import org.msgpack.rpc.Request;

import junit.framework.TestCase;

public class TestPeerServer extends TestCase{
	public static class PeerHandler{
		public void heartBeat(Request req){
			MessagePackObject obj = req.getArguments();
			MessagePackObject[] objs = obj.asArray();

			if(objs[0] != null && objs[0].isRawType()){
				System.out.println(objs[0].asString());
			}
			req.sendResult(RawType.create("Beat!"));
			return;
		}
	}

	public static class CallBack implements Runnable{
		@Override
		public void run(){
			System.out.print("!");
		}
	}

	public static void testPeerServer() throws InterruptedException, IOException{

		// Server srv = new Server(new EventLoop());
		// srv.serve(new ReflectionDispatcher(new PeerHandler()));
		// srv.listen(30001);

		Client cli = new Client("127.0.0.1", 30001);
		cli.setRequestTimeout(1);
		for(int i = 0; i < 100; i++){
			Future<MessagePackObject> future = cli.callAsyncApply("heartBeat",
					new Object[]{ RawType.create("Beat?") });
			MessagePackObject obj = future.get();
			if(obj != null && obj.isRawType()){
				System.out.println(obj.asString());
			}

			Thread.sleep(900);
		}
		cli.close();
		// srv.close();
	}
}
